package com.leejxx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leejxx.article.feign.ArticleFeign;
import com.leejxx.common.constants.user.AdminConstants;
import com.leejxx.model.article.pojos.ApAuthor;
import com.leejxx.model.common.dto.PageResponseResult;
import com.leejxx.model.common.dto.ResponseResult;
import com.leejxx.model.common.enums.AppHttpCodeEnum;
import com.leejxx.model.user.dtos.AuthDto;
import com.leejxx.model.user.pojos.ApUser;
import com.leejxx.model.user.pojos.ApUserRealName;
import com.leejxx.model.wemedia.pojos.WmUser;
import com.leejxx.user.dao.ApUserDao;
import com.leejxx.user.dao.ApUserRealNameDao;
import com.leejxx.user.service.ApUserRealNameService;
import com.leejxx.wemedia.feign.WmUserFeign;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author LeeJx
 * @ClassName ApUserRealNameServiceImpl
 * @Description TODO
 * @date 2021/2/20 1:15
 * @Version 1.0
 */
@Service
@Transactional
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameDao, ApUserRealName> implements ApUserRealNameService {
    @Autowired
    private ApUserDao apUserDao;

    @Autowired
    private WmUserFeign wmUserFeign;

    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        if (null == authDto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        authDto.checkParam();

        Page<ApUserRealName> page = new Page<>(authDto.getPage(), authDto.getSize());
        LambdaQueryWrapper<ApUserRealName> queryWrapper = new LambdaQueryWrapper<>();
        if (authDto.getStatus() != null) {
            queryWrapper.eq(ApUserRealName::getStatus, authDto.getStatus());
        }

        Page<ApUserRealName> pageResult = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        if (null == dto || null == status) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealName apUserRealName = new ApUserRealName();
        apUserRealName.setId(dto.getId());
        apUserRealName.setStatus(status);
        if (StringUtils.isNotBlank(dto.getMsg())) {
            apUserRealName.setReason(dto.getMsg());
        }

        updateById(apUserRealName);
        if (status.equals(AdminConstants.PASS_AUTH)) {
            Integer userRealNameId = dto.getId();
            ApUserRealName userRealName = getById(userRealNameId);
            ApUser apUser = apUserDao.selectById(userRealName.getUserId());
            if (apUser == null) {
                ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }

            //创建自媒体用户
            WmUser wmUser = wmUserFeign.findByName(apUser.getName());
            if (null == wmUser) {
                wmUser = new WmUser();
                wmUser.setName(apUser.getName());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setPhone(apUser.getPhone());
                wmUser.setCreatedTime(new Date());
                wmUser.setApUserId(apUser.getId());
                wmUser.setStatus(9);
                wmUserFeign.save(wmUser);
            }

            //创建作者
            ApAuthor apAuthor = articleFeign.findByUserId(wmUser.getApUserId());
            if(apAuthor == null){
                apAuthor = new ApAuthor();
                apAuthor.setName(wmUser.getName());
                apAuthor.setUserId(wmUser.getApUserId());
                apAuthor.setType(2);
                apAuthor.setCreatedTime(new Date());
                articleFeign.save(apAuthor);
            }
            apUser.setFlag((short) 1);
            apUserDao.updateById(apUser);
            return null;
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
